package com.example.readerapp.myandserver.mybookconfig;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import org.docx4j.wml.P;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class BookInfoTable
{
   SQLiteDatabase database;
   /*表名*/
   private final String tableName="book_info";
   /*属性名称*/
    private final String bookName="bookName";
    private final String currentPageNumber="currentPageNumber";
    private final String backgroundColor="backgroundColor";
    private final String fontColor="fontColor";
    private final String fontSize="fontSize";
    private final String position="position";

    private final String id="id";
   public BookInfoTable()
   {
       database=MyDatabase.getMyDatabase();//不在这里赋值可能会出问题
       if(database==null)
       {}
   }
   public void creatBookInfoTable()
   {
      String sql_CreateTable="create table if not exists "+tableName+"("
              +this.id+" integer primary key autoincrement,"
              +this.bookName+" text,"
              +this.currentPageNumber+" integer,"
              +this.backgroundColor+" text default \"black\","
              +this.fontColor+" text default \"rgba(256, 256, 256, 0.6)\","
              +this.position+" text default \"0\","
              +this.fontSize+" text default \"15px\""+")";
      try
      {
          System.out.println("执行sql"+sql_CreateTable);
          database.execSQL(sql_CreateTable);
      }catch (Exception e)
      {
          MyDatabase.initMyDatabase();
          database.execSQL(sql_CreateTable);
      }
   }
   //判断是否已经建表
   public boolean exists(){
      String sql="SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName+"';";
      Cursor cursor= database.rawQuery(sql,null);
      if(cursor.moveToFirst())
      {
          cursor.close();
          return true;
      }
      cursor.close();
      return false;

   }

  public void insertBookInfo(String bookName,int currentPageNumber,String backgroundColor,String fontColor)
   {

      ContentValues contentValues=new ContentValues();
      contentValues.put(this.bookName,bookName);
      contentValues.put(this.currentPageNumber,currentPageNumber);
       contentValues.put(this.backgroundColor,backgroundColor);
       contentValues.put(this.fontColor,fontColor);

       try
    {
        database.insert(tableName, null, contentValues);
    }catch (Exception e)
    {
        MyDatabase.initMyDatabase();
        database.insert(tableName, null, contentValues);
    }

   }
    public void insertBookInfo(String bookName,int currentPageNumber)
    {

        String sql="insert into "+this.tableName+"("+this.bookName+","+this.currentPageNumber+") "+" values(\""+bookName+"\","+currentPageNumber+")";

     //   ContentValues contentValues=new ContentValues();
       // contentValues.put(this.bookName,bookName);
        //contentValues.put(this.currentPageNumber,currentPageNumber);
        try
        {
            System.out.println("执行sql"+sql);
            database.execSQL(sql);
          //  database.insert(tableName, null, contentValues);
        }catch (Exception e)
        {
            MyDatabase.initMyDatabase();
            database.execSQL(sql);
            //database.insert(tableName, null, contentValues);
        }

    }
   public void updateCurrentPageNumberByBookName(String bookName,int currentPageNumber)
   {
       String sql="update "+tableName+" set "+this.currentPageNumber+"="+currentPageNumber+" where "+this.bookName+"=\""+bookName+"\"";
       try
       {
           database.execSQL(sql);
       }catch (Exception e)
       {
           MyDatabase.initMyDatabase();
           database.execSQL(sql);
       }

   }
   @SuppressLint("Range")
   public int findCurrentPageNumberByBookName(String bookName)
   {
       String sql="select "+currentPageNumber+" from "+tableName+" where "+this.bookName+"=\""+bookName+"\"";
       int currentPageNumber=1;
       Cursor cursor=database.rawQuery(sql,null);//用原生sql查询
       if(cursor.moveToFirst())
       {
           currentPageNumber=cursor.getInt(cursor.getColumnIndex(this.currentPageNumber));
           //System.out.print("查询得当前页码:"+currentPageNumber+"\n");
       }
       cursor.close();
       return currentPageNumber;


   }
   @SuppressLint("Range")
   public String findBackgroundColorByBookName(String bookName)
   {
       String sql="select "+this.backgroundColor+" from "+tableName+" where "+this.bookName+"=\""+bookName+"\"";
       String backgroundColor="";
       Cursor cursor=database.rawQuery(sql,null);//用原生sql查询
       if(cursor.moveToFirst())
       {
           backgroundColor=cursor.getString(cursor.getColumnIndex(this.backgroundColor));
           //System.out.print("查询得当前页码:"+currentPageNumber+"\n");
       }
       cursor.close();
       return backgroundColor;
   }
    @SuppressLint("Range")
    public String findFontColorByBookName(String bookName)
    {
        String sql="select "+this.fontColor+" from "+tableName+" where "+this.bookName+"=\""+bookName+"\"";
        String fontColor="";
        Cursor cursor=database.rawQuery(sql,null);//用原生sql查询
        if(cursor.moveToFirst())
        {
            fontColor=cursor.getString(cursor.getColumnIndex(this.fontColor));
            //System.out.print("查询得当前页码:"+currentPageNumber+"\n");
        }
        cursor.close();
        return fontColor;
    }
    public void updateBackgroundColorByBookName(String bookName,String backgroundColor)
    {
        String sql="update "+tableName+" set "+this.backgroundColor+"=\""+backgroundColor+"\" where "+this.bookName+"=\""+bookName+"\"";
        try
        {
            database.execSQL(sql);
          //  System.out.println("执行sql"+sql);
        }catch (Exception e)
        {
            MyDatabase.initMyDatabase();
            database.execSQL(sql);
        }

    }
    public void updateFontColorByBookName(String bookName,String fontColor)
    {
        //注意fontColor需用"包围
        String sql="update "+tableName+" set "+this.fontColor+"=\""+fontColor+"\" where "+this.bookName+"=\""+bookName+"\"";
        try
        {
            database.execSQL(sql);
        }catch (Exception e)
        {
            MyDatabase.initMyDatabase();
            database.execSQL(sql);
        }

    }
    @SuppressLint("Range")
    public String findFontSizeByBookName(String bookName)
    {
        String sql="select "+this.fontSize+" from "+tableName+" where "+this.bookName+"=\""+bookName+"\"";
        String fontSize="";
        Cursor cursor=database.rawQuery(sql,null);//用原生sql查询
        if(cursor.moveToFirst())
        {
            fontSize=cursor.getString(cursor.getColumnIndex(this.fontSize));
            System.out.print("查询得当前页码:"+currentPageNumber+"\n");
        }
        cursor.close();
        return fontSize;
    }
    public void updateFontSizeByBookName(String bookName,String fontSize)
    {
        String sql="update "+tableName+" set "+this.fontSize+"=\""+fontSize+"\" where "+this.bookName+"=\""+bookName+"\"";
        try
        {
            database.execSQL(sql);
        }catch (Exception e)
        {
            MyDatabase.initMyDatabase();
            database.execSQL(sql);
        }

    }
    public boolean findBookByBookName(String bookName)
    {
        String sql="select "+this.bookName+" from "+tableName+" where "+this.bookName+"=\""+bookName+"\"";
        boolean find=false;
        Cursor cursor=database.rawQuery(sql,null);//用原生sql查询
        if(cursor.moveToFirst())
        {
            find=true;
            //System.out.print("查询得当前页码:"+currentPageNumber+"\n");
        }
        cursor.close();
        return find;
    }
   public void deleteInfoByBookName(String bookName)
   {
       String sql="delete from "+tableName+" where "+this.bookName+"=\""+bookName+"\"";
       database.execSQL(sql);
   }
   public HashMap<Integer,String[]> queryAll()
   {
      HashMap<Integer,String[]> r=new HashMap<>();
      Cursor cursor=database.query("book_info", null, null, null, null, null, null);
      if(cursor.moveToFirst())
      {
         do{
             @SuppressLint("Range") int id= cursor.getInt(cursor.getColumnIndex(this.id));
             System.out.print("id:"+id);
             @SuppressLint("Range") String name= cursor.getString(cursor.getColumnIndex(this.bookName));
            System.out.print(" 书名："+name);
            @SuppressLint("Range") int c=cursor.getInt(cursor.getColumnIndex(this.currentPageNumber));
             System.out.print(" 当前页码:"+c+"\n");
             @SuppressLint("Range") String bc=cursor.getString(cursor.getColumnIndex(this.backgroundColor));
             System.out.print(" 背景颜色:"+bc+"\n");
             @SuppressLint("Range") String fc=cursor.getString(cursor.getColumnIndex(this.fontColor));
             System.out.print(" 字体颜色:"+fc+"\n");
             @SuppressLint("Range") String fz=cursor.getString(cursor.getColumnIndex(this.fontSize));
             System.out.print(" 字体大小:"+fz+"\n");
             @SuppressLint("Range") String pos=cursor.getString(cursor.getColumnIndex(this.position));//注意修改列名8
             System.out.print(" 位置:"+pos+"\n");

             String[] a=new String[]{name, String.valueOf(c),bc,fc,fz};
            r.put(id,a);
         }while (cursor.moveToNext());
      }
      cursor.close();
      return r;
   }
   public void deleteAll(){//注意formatBookUtil.consistency()
       String sql="delete from "+tableName;
       database.execSQL(sql);
   }
   public void deleteTable()
   {
       String sql="drop table "+tableName;
       database.execSQL(sql);
   }
}